/**
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐▒
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐▒
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐▒
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌▒
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * 单身狗就这样默默地看着你，一句话也不说。
 */
package com.example;

import com.lutao.util.BaiduCharacterRecognitionUtil;
import com.lutao.util.BaiduPictureRecognitionUtil;
import com.lutao.util.IoUtil.UrlToBytes;
import com.lutao.util.YoutuUtil;
import net.sf.json.JSONObject;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.xihc.jvminfo.EnableJvmMonitor;

import java.util.HashMap;
import java.util.Map;

@Controller
@EnableAutoConfiguration
//@SpringBootApplication
@EnableJvmMonitor
public class HelloController {

	@RequestMapping("/hello")
    @ResponseBody
    String home() {
        return "Hello ,spring boot!";
    }
    @RequestMapping("/imgRecognition")
    @ResponseBody
    public Object imgRecognition(@RequestBody String objectJson){
        Map<String,Object> paramMap = null,map = new HashMap<String, Object>();
        //Map<String,Object> map =BaiduPictureRecognitionUtil.pictureRecognition("C:\\animal.jpg",0);
        JSONObject object = JSONObject.fromObject(objectJson);
        paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(object), Map.class);
        byte[] file = UrlToBytes.getImageFromNetByUrl(paramMap.get("fileSrc").toString());
        Integer type = (Integer) paramMap.get("type");
        map = BaiduPictureRecognitionUtil.pictureRecognition(file,type);
        return map;
    }
    @RequestMapping("/textRecognition")
    @ResponseBody
    public Object textRecognition(@RequestBody String objectJson){
        Map<String,Object> paramMap = null,map = new HashMap<String, Object>();
        //Map<String,Object> map =BaiduPictureRecognitionUtil.pictureRecognition("C:\\animal.jpg",0);
        JSONObject object = JSONObject.fromObject(objectJson);
        paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(object), Map.class);
        byte[] file = UrlToBytes.getImageFromNetByUrl(paramMap.get("fileSrc").toString());
        Integer type = (Integer) paramMap.get("type");
        map = BaiduCharacterRecognitionUtil.characterRecognition(file,type);
        return map;
    }
    @RequestMapping("/faceMerge")
    @ResponseBody
    public Object faceMerge(@RequestBody String objectJson){
        Map<String,Object> paramMap = null,map = new HashMap<String, Object>();
        //Map<String,Object> map =BaiduPictureRecognitionUtil.pictureRecognition("C:\\animal.jpg",0);
        JSONObject object = JSONObject.fromObject(objectJson);
        paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(object), Map.class);
        byte[] file = UrlToBytes.getImageFromNetByUrl(paramMap.get("fileSrc").toString());
        String model = paramMap.get("model").toString();
        map = YoutuUtil.faceMerge(file,model);
        return map;
    }
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                //Due to CONFIDENTIAL and /*, this will cause Tomcat to redirect every request to HTTPS.
                //You can configure multiple patterns and multiple constraints if you need more control over what is and is not redirected.

                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

        //Set the scheme that will be assigned to requests received through this connector
        //@param scheme The new scheme
        connector.setScheme("http");

        //Set the port number on which we listen for requests.
        // @param port The new port number
        connector.setPort(80);

        //Set the secure connection flag that will be assigned to requests received through this connector.
        //@param secure The new secure connection flag
        //if connector.setSecure(true),the http use the http and https use the https;else if connector.setSecure(false),the http redirect to https;
        connector.setSecure(true);

        //redirectPort The redirect port number (non-SSL to SSL)
        connector.setRedirectPort(443);
        return connector;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloController.class, args);
        //运行之后在浏览器中访问：http://localhost:8080/hello
    }
	
}
